package com.ciaojian.store.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ciaojian.core.enums.business.OrderStatusEnum;
import com.ciaojian.core.model.Order;
import com.ciaojian.core.model.dto.WorkerDTO;
import com.ciaojian.core.model.vo.WorkerPriceVo;
import com.ciaojian.core.util.Result;
import com.ciaojian.store.service.OrderServiceImpl;
import com.ciaojian.store.service.SchedulingServiceImpl;
import com.ciaojian.store.service.WorkerServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.Date;
import java.util.List;

/**
 * @author Atlakyn
 */
@Api(tags = "发型师")
@RestController
@RequestMapping("/worker")
public class WorkerController {
    @Resource
    private WorkerServiceImpl workerService;
    @Resource
    private SchedulingServiceImpl schedulingService;
    @Resource
    private OrderServiceImpl orderService;

    @ApiOperation("发型师每日顾客属性数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "storeId", value = "门店Id", paramType = "form"),
            @ApiImplicitParam(name = "workerId", value = "发型师Id", paramType = "form"),
            @ApiImplicitParam(name = "pageNo", value = "页码", paramType = "form"),
            @ApiImplicitParam(name = "pageSize", value = "页面数量", paramType = "form"),
            @ApiImplicitParam(name = "beginDate", value = "开始时间", paramType = "form"),
            @ApiImplicitParam(name = "endDate", value = "结束时间", paramType = "form")
    })
    @GetMapping("/customer-type")
    public Result workerCustomerTypeEveryday(@NotNull(message = "门店Id") Integer storeId,
                                             @NotNull(message = "发型师Id") Integer workerId,
                                             @RequestParam(defaultValue = "0") Integer pageNo,
                                             @RequestParam(defaultValue = "6") Integer pageSize,
                                             @NotNull(message = "开始时间不能为空") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date beginDate,
                                             @NotNull(message = "结束时间不能为空") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date endDate) {
        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("count(customer_type = 0 OR NULL) AS stranger," +
                        "       count(customer_type = 1 OR NULL) AS familiar," +
                        "       count(customer_type = 2 OR NULL) AS deepFamiliar," +
                        "       count(customer_type = 3 OR NULL) AS first," +
                        "       date_format(add_time, '%Y-%m-%d') AS day")
                .groupBy("date_format(add_time, '%Y-%m-%d')")
                .orderByDesc("date_format(add_time, '%Y-%m-%d')")
                .lambda()
                .eq(Order::getStoreId, storeId)
                .in(Order::getStatus, OrderStatusEnum.paid, OrderStatusEnum.complete)
                .eq(Order::getWorkerId, workerId)
                .between(Order::getAddTime, beginDate, endDate);
        return Result.ok(orderService.pageMaps(new Page<>(pageNo, pageSize), queryWrapper));
    }

    @ApiOperation("发型师面板")
    @GetMapping("/board/{storeId}")
    public Result getWorkerBoard(@PathVariable Integer storeId) {
        List<WorkerDTO> workerList = workerService.getBaseMapper().getWorkerByStoreId(storeId);
        workerList.forEach(workerDTO -> workerDTO.setSchedule(schedulingService.getBaseMapper().findByWorkerAndCurDate(workerDTO.getId())));
        return Result.ok(workerList);
    }

    @ApiOperation("发型师业绩数据统计")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "storeId", value = "门店ID,传0为查询全部门店", paramType = "path"),
            @ApiImplicitParam(name = "startDate", value = "开始日期", paramType = "form"),
            @ApiImplicitParam(name = "endDate", value = "结束日期", paramType = "form"),
            @ApiImplicitParam(name = "orderTypes", value = "订单类型,多个类型用 , 隔开", paramType = "form"),
            @ApiImplicitParam(name = "pageNo", value = "页码", paramType = "form"),
            @ApiImplicitParam(name = "pageSize", value = "页面数量", paramType = "form")
    })
    @GetMapping("/{storeId}/data")
    public Result getWorkerInfo(@PathVariable("storeId") Integer storeId,
                                @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date startDate,
                                @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date endDate,
                                @RequestParam List<Integer> orderTypes,
                                @RequestParam(defaultValue = "0") Integer pageNo,
                                @RequestParam(defaultValue = "6") Integer pageSize) {
        return Result.ok(workerService.findWorkerData(storeId, startDate, endDate, orderTypes, new Page<>(pageNo, pageSize)));
    }

    @ApiOperation("发型师会员数据统计")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "storeId", value = "门店ID,传0为查询全部门店", paramType = "path"),
            @ApiImplicitParam(name = "startDate", value = "开始日期", paramType = "form"),
            @ApiImplicitParam(name = "endDate", value = "结束日期", paramType = "form"),
            @ApiImplicitParam(name = "pageNo", value = "页码", paramType = "form"),
            @ApiImplicitParam(name = "pageSize", value = "页面数量", paramType = "form")
    })
    @GetMapping("/{storeId}/member-order/data")
    public Result getMemberOrderInfo(@PathVariable("storeId") Integer storeId,
                                     @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date startDate,
                                     @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date endDate,
                                     @RequestParam(defaultValue = "0") Integer pageNo,
                                     @RequestParam(defaultValue = "6") Integer pageSize) {
        return Result.ok(workerService.findMemberOrderData(storeId, startDate, endDate, new Page<>(pageNo, pageSize)));
    }
}
